package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.mapper.AlbumStatMapper;
import com.atguigu.tingshu.album.mapper.TrackStatMapper;
import com.atguigu.tingshu.album.service.MqOpsService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.client.OrderInfoFeignClient;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * @Author：yyj
 * @Package：com.atguigu.tingshu.album.service.impl
 * @Project：tingshu_parent240829
 * @name：MqOpsServiceImpl
 * @Date：2025/2/5 20:01
 * @Filename：MqOpsServiceImpl
 */
@Service
@Slf4j
public class MqOpsServiceImpl implements MqOpsService {
    @Autowired
    private AlbumStatMapper albumStatMapper;
    @Autowired
    private TrackStatMapper trackStatMapper;
    @Autowired
    private OrderInfoFeignClient orderInfoFeignClient;
    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void listenAlbumAndTrackStatNumUpdate(TrackStatMqVo trackStatMqVo) {
        Long albumId = trackStatMqVo.getAlbumId();
        Long trackId = trackStatMqVo.getTrackId();
        Integer count = trackStatMqVo.getCount();
        String statType = trackStatMqVo.getStatType();
        try {
            if (albumId != null) {
                //修改专辑播放量
                int a = albumStatMapper.updateAlbumStatNum(albumId, statType, count);
                log.info("修改专辑播放量：{}", a > 1 ? "success" : "fail");
                //修改声音播放量
                int t = trackStatMapper.updateTrackStatNum(trackId, SystemConstant.TRACK_STAT_PLAY, count);
                log.info("修改声音播放量：{}", t > 1 ? "success" : "fail");
            } else {
                //修改声音播放量
                int t = trackStatMapper.updateTrackStatNum(trackId, statType, count);
                if (t > 0) {
                    log.info("修改成功");
                } else {
                    log.info("修改失败");
                }
            }
        } catch (Exception e) {
            throw new GuiguException(201, "数据库操作失败");
        }
    }

    @Override
    public void updateAlbumAndBuyNum(String orderNo) {
        // 1.根据订单编号查询订单信息获取订单类型
        Result<OrderInfo> orderInfoResult = orderInfoFeignClient.getOrderInfoByOrderNo(orderNo);
        OrderInfo orderInfoData = orderInfoResult.getData();
        Assert.notNull(orderInfoData, "远程查询订单信息失败");
        // 2.订单的类型
        String itemType = orderInfoData.getItemType();
        Long albumId = null;
        // 买的是专辑（修改播放量）
        if ("1001".equals(itemType)) {
            albumId = orderInfoData.getOrderDetailList().get(0).getItemId();
        }
        // 买的是声音（修改专辑播放量）
        else if ("1002".equals(itemType)) {
            Long trackId = orderInfoData.getOrderDetailList().get(0).getItemId();
            Result<TrackInfo> trackInfoResult = albumInfoFeignClient.getTrackInfoById(trackId);
            TrackInfo trackInfoData = trackInfoResult.getData();
            Assert.notNull(trackInfoData, "远程查询声音信息失败");
            albumId = trackInfoData.getAlbumId();
        }
        // 买的是vip(es不用处理)
        else {

        }

        //修改mysql专辑的购买量
        albumStatMapper.updateAlbumStatNum(albumId,"0403",orderInfoData.getOrderDetailList().size());
    }
}